if((typeof mrObjs) == 'undefined') 
    var mrObjs = {};

function MRSelect(select, list){
    var _this = this;
    list = list || [];
    var selectedIndex = null;
    
    var $children = $(select).children('option');
    if($children.length > 0){
        list = [];
        $children.each(function(index){
            list.push($(this).val());
            if($(this).attr('selected')){
                selectedIndex = index;
            }
        });
    }
    $children = null;
    
    
    
    this.length = function(){
        return list.length;
    }
    
    this.removeOption = function(index){
        if(isNaN(index)) index = list.length -1;
        if(index < 0) index = list.length + index; //We are going to allow index below 0 like Python :D
        //index validation
        if(index < 0 || index >= list.length){
            throw "index out of bounds";
        }
        list.splice(index, 1);
    }
    
    this.removeAllOptions = function(){
        list = [];
    }
    
    this.addOption = function(option, index){
        if(isNaN(index)) index = list.length;
        if(index < 0) index = list.length + index; //We are going to allow index below 0 like Python :D
        //index validation
        if(index < 0 || index > list.length){
            throw "index out of bounds";
        }
        list.splice(index, 0, option);
    };
    
    this.replaceOption = function(option, index){
        this.removeOption(index);
        this.addOption(option, index);
    };
    
    this.replaceAllOptions = function(option){
        this.removeAllOptions();
        this.addOption(option, 0);
    };
};    
    
$(function(){
    $('select.mrselect').each(function(){
        var id = $(this).attr('id');
        var a = new MRSelect(this);
        if(id){
            mrObjs[id] = a;
        }
    });
});

